use std::convert::TryFrom;
use std::convert::TryInto;

#[derive(Debug,PartialEq)]
struct EvenNumber(i32);

impl TryFrom<i32> for EvenNumber {
    type Error = ();

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        if value % 2 == 0 {
            Ok(EvenNumber(value))
        }else{
            Err(())
        }
    }
}

pub fn example() {
    //Try From
    assert_eq!(EvenNumber::try_from(8),Ok(EvenNumber(8)));
    assert_eq!(EvenNumber::try_from(5),Err(()));
    //Try Into 主要用于易出错的转换，所以其返回值是Result型
    /*
    pub enum Result<T, E> {
    Ok(T),
    Err(E),
    }
    */
    let result: Result<EvenNumber,()> = 8i32.try_into();
    assert_eq!(result,Ok(EvenNumber(8)));
    let result: Result<EvenNumber,()> = 5i32.try_into();
    assert_eq!(result,Err(()));

}